// --------------------------------------------------------------------
//   name 	:  BBCode plugin
//   desc 	:  Adds the BBcode box on top of text fields
//   version 	: 
//   dev 	: 
// --------------------------------------------------------------------
// DEPENDENCIES
// 
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1 	Initial Release
// 0.1-0.9I   Various bug fixes. Final release hopefully
// v1.0		Officially releasing this.
// --------------------------------------------------------------------
plugins.BBCodesTextArea = {
	"BBMenuGenerator" : function() {
		var random = new Date;
		random = random.getTime();
		var xhtml = '<center><table style="cursor:pointer;" cellpadding="0" cellspacing="0">' +
			'<tr>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'player\','+random+',\'NULL\');" title="Insert player tag" tabindex="10"><img src="graphic/face.png" alt="Player" /></td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'village\','+random+',\'NULL\');" title="Insert village tag" tabindex="11"><img src="graphic/buildings/main.png" alt="Village" /></td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'tribe\','+random+',\'NULL\');" title="Insert tribe tag" tabindex="12"><img src="graphic/command/support.png" alt="Tribe" /></td>' +
			'<td style="cursor:default;">.</td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'b\','+random+',\'button1\');" title="Make text bold" tabindex="13" style="width:15px;text-align:center;"><div style="float:left;"><b>B</b></div><div id="button1" style="float:left;font-family:arial;size:10px;color:red;"></div></td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'i\','+random+',\'button2\');" title="Make text italic" tabindex="14" style="width:15px;text-align:center;"><div style="float:left;"><i>I</i></div><div id="button2" style="float:left;font-family:arial;size:10px;color:red;"></div></td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'u\','+random+',\'button3\');" title="Underline the text" tabindex="15" style="width:15px;text-align:center;"><div style="float:left;"><u>U</u></div><div id="button3" style="float:left;font-family:arial;size:10px;color:red;"></div></td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'s\','+random+',\'button4\');" title="Underline the text" tabindex="15" style="width:15px;text-align:center;"><div style="float:left;"><s style="text-decoration:line-through;"><del style="text-decoration:line-through;">S</del></s></div><div id="button4" style="float:left;font-family:arial;size:10px;color:red;"></div></td>' +
			'<td style="cursor:default;">.</td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'quote\','+random+',\'button5\');" title="Insert quote" tabindex="16" style="width:22px;"><div style="float:left;"><img src="http://www.offthemap.com/images/site/blockquote.jpg" alt="Quote" /></div><div id="button5" style="float:left;font-family:arial;size:10px;color:red;"></div></td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'url\','+random+',\'button6\');" title="Insert link" tabindex="17" style="width:22px;"><div style="float:left;"><img src="http://runbut.com/Images/Hyperlink.jpg" alt="URL" /></div><div id="button6" style="float:left;font-family:arial;size:10px;color:red;"></div></td>' +
				'<td onmouseover="javascript:this.style.backgroundColor=\'peachpuff\';" onmouseout="javascript:this.style.background=\'none\';" onclick="javascript:insertBB(\'img\','+random+',\'button7\');" title="Insert img" tabindex="18" style="width:22px;"><div style="float:left;"><img src="http://www.zaburi.com/images/icons/image_add.png" alt="Image" /></div><div id="button7" style="float:left;font-family:arial;size:10px;color:red;"></div></td>' +
			'<td style="cursor:default;">.</td>' +
				'<td title="Insert colour"><select onchange="javascript:insertBB(\'color\','+random+',this.value);this.selectedIndex=0;"><option value="DISABLED" selected disabled>Font Color</option><option value="red" style="color:red;">Red</option><option value="pink" style="color:pink;">Pink</option><option value="purple" style="color:purple;">Purple</option><option value="blue" style="color:blue;">Blue</option><option value="aqua" style="color:aqua;">Aqua</option><option value="green" style="color:green;">Green</option><option value="yellow" style="color:yellow;">Yellow</option><option value="orange" style="color:orange;">Orange</option><option value="black" style="color:black;">Black</option><option value="gray" style="color:gray;">Gray</option><option value="silver" style="color:silver;">Silver</option><option value="white" style="color:white;">White</option></select></td>' +
				'<td title="Insert size"><select onchange="javascript:insertBB(\'size\','+random+',this.value);this.selectedIndex=0;" style="height:17px;"><option value="DISABLED" selected disabled>Font Size</option><option value="2" style="font-size:2pt;">2</option><option value="4" style="font-size:4pt;">4</option><option value="6" style="font-size:6pt;">6</option><option value="8" style="font-size:8pt;">8</option><option value="10" style="font-size:10pt;">10</option><option value="12" style="font-size:12pt;">12</option><option value="14" style="font-size:14pt;">14</option><option value="16" style="font-size:16pt;">16</option><option value="18" style="font-size:18pt;">18</option><option value="20" style="font-size:20pt;">20</option></select></td>' +
			'<td style="cursor:default;">.</td>' +
				'<td><img onclick="javascript:toggleEmotes();" src="http://forum.tribalwars.net/images/smilies/smile.gif" /></td>' +
			'</tr><tr>' +
			'<td colspan="20" style="text-align:center;height:1px;" align="center"><div id="emoteTD" style="text-align:center;display:'+( getValuePlugin("ShowEmotesByDefault")?'block':'none' )+';">' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_lol.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/smile.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_idea.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_wink.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_evil.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_twisted.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_eek.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_surprised.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_cry.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_smile.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_cool.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_sad.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_confused.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_rolleyes.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_biggrin.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_redface.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_razz.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://forum.tribalwars.net/images/smilies/phpbb_smilies/icon_neutral.gif" />' +
			'<br />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://avatares.miarroba.com/src/1450838/2c8c8416.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://avatares.miarroba.com/src/1450838/bb8a41b2.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Amor/16-MundoEmoti-Amor.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Amor/12-MundoEmoti-Amor.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Animales/23-MundoEmoti-Animales.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Animales/02-MundoEmoti-Animales.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Animales/24-MundoEmoti-Animales.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/13-MundoEmoti-Simbolos.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/70-MundoEmoti-Simbolos.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/17-MundoEmoti-Simbolos.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/38-MundoEmoti-Simbolos.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/24-MundoEmoti-Simbolos.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/04-MundoEmoti-Simbolos.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/33-MundoEmoti-Simbolos.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/09-MundoEmoti-Simbolos.gif" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/15-MundoEmoti-Simbolos.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.mundoemoti.com/Emoticones/Simbolos/14-MundoEmoti-Simbolos.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.emoticonesanimados.com.ar/img/d3fc8a1e5d60581f9008ace99cfcf60e.gif" />' +
			'<br />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_spear.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_sword.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_axe.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_archer.png" />' +
				'&nbsp;&nbsp;.&nbsp;&nbsp;<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_spy.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_light.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_marcher.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_heavy.png" />' +
				'&nbsp;&nbsp;.&nbsp;&nbsp;<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_ram.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_catapult.png" />' +
				'&nbsp;&nbsp;.&nbsp;&nbsp;<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_knight.png" />' +
				'&nbsp;&nbsp;.&nbsp;&nbsp;<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/unit/unit_snob.png" />' +
				'&nbsp;&nbsp;.&nbsp;&nbsp;<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/holz.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/lehm.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/eisen.png" />' +
				'<img onclick="javascript:insertBB(\'emote\','+random+',this.src);" src="http://www.tribalwars.net/graphic/res.png" />' +
			'</div></td>' +
			'</tr>' +
			'</table></center>'; 
				      
      	document.body.innerHTML = document.body.innerHTML.replace( /<textarea\s/gi, xhtml+"<textarea id=\"txt_"+random+"\" ");
      	
		setFunc("toggleEmotes", function(){
			$('#emoteTD').slideToggle('slow');
		});
		
		setFunc("insertBB", function(insertType, ident, thirdVar){
      
			txt = document.getElementById("txt_"+ident);
      
			var start = txt.selectionStart;
			var end   = txt.selectionEnd;
			var txtlength = 0;
			var selection = '';
			var selectionBefore = '';
			var selectionAfter = '';
      
			var BBCodeSet = {
				"player":new Array("P","[player]","[/player]"),
				"village":new Array("V","[village]","[/village]"),
				"tribe":new Array("A","[ally]","[/ally]"),
				
				"b":new Array("B","[b]","[/b]"),
				"i":new Array("I","[i]","[/i]"),
				"u":new Array("U","[u]","[/u]"),
				"s":new Array("S","[s]","[/s]"),
				
				"quote":new Array("QU","[quote=AUTHOR]","[/quote]"),
				"url":new Array("UR","[url]","[/url]"),
				"img":new Array("IM","[img]","[/img]"),
				
				"color":new Array("CO","[color="+thirdVar+"]","[/color]"),
				"size":new Array("SI","[size="+thirdVar+"]","[/size]"),
				
				"emote":new Array("EM","[img]"+thirdVar+"[/img]","")
			}

			if (start == end) {
				if(thirdVar.indexOf("button")!=-1) {
				var ButtonID = parseInt(thirdVar.charAt(6));
					if(document.getElementById(thirdVar).innerHTML!="*") {
						txt.value = txt.value.substr(0, start) + BBCodeSet[insertType][1] + txt.value.substr(end, txt.value.length);
						document.getElementById(thirdVar).innerHTML="*"
					} else {
						txt.value = txt.value.substr(0, start) + BBCodeSet[insertType][2] + txt.value.substr(end, txt.value.length);
						document.getElementById(thirdVar).innerHTML=""
					}
				} else {
					txt.value = txt.value.substr(0, start) + BBCodeSet[insertType][1] + BBCodeSet[insertType][2] + txt.value.substr(end, txt.value.length);
				}
			} else {
      			txtlength = txt.value.length;
      			selection = txt.value.substr(start, (end - start));
      			selectionBefore = txt.value.substr(0, start);
      			selectionAfter = txt.value.substr(end, txtlength);
      
      			if (BBCodeSet[insertType][0] == 'V' && selection.match(/(\d+){3}([\/|]+){1}(\d+){3}/gi)) {
      				selection = selection.replace(/(.*)(\d+)(\d+)(\d+)([\/|]+){1}(\d+)(\d+)(\d+)(.*)/gi, "$2$3$4|$6$7$8");
      			}
      
      			txt.value = selectionBefore + BBCodeSet[insertType][1] + selection + BBCodeSet[insertType][2] + selectionAfter;
      		}
		});
		return true;
	},
	'enhance_forum' : function() {
		plugins.BBCodesTextArea.BBMenuGenerator();
	},
	'enhance_game' : function() {
		if(getValuePlugin("ShowTribeStarLink")==true) $('#menu_row').html( $('#menu_row').html() + "<td><a href='game.php?village="+personals.getCurrentVillage()+"&screen=ally&mode=forum' title='Tribe Forum'><img src='http://en15.tribalwars.net/graphic/ally_forum.png' /></a></td>" );
		if(location.href.match( /screen=settings/) || location.href.match( /screen=mail/ )) plugins.BBCodesTextArea.BBMenuGenerator();
	},
	'info' : {
		'mandatory' : false,
		'name' : "Toolbar on top of TextAreas + Tribe forum star link",
		'desc' : "Adds a BB toolbar on top of TextAreas in Mail, Profile and Forum, Now with Emotes",
		'version' : "v1.0",
		'dev' : "JelianSD"
	},
	'variables' : {
		'tooltip':["Check the box to show the tribe forum star image.","Check the box to show the emoticons by default."],
		'desc':["Disable or show the top right forum star image.","Show or hide the emoticons tab by default."],
		'ShowTribeStarLink':true,
		'ShowEmotesByDefault':false,
	}
}
